const { URL } = require('url');

const querystring = require('querystring');

// 1.1 create a new URL object                                                                                                                                                                                                                        
const url = new URL('https://www.example.com/path/to/page.html?query=string#hash');
console.log(url)

//1.2 parse a string to a URL object
const url2 = new URL('https://www.example.com/path/to/page.html?query=string#hash');
console.log(url2)                                                                                                                                                                                                                                     
// 1.3 get the properties of a URL object
console.log(url.href); // https://www.example.com/path/to/page.html?query=string#hash
console.log(url.protocol); // https:
console.log(url.host); // www.example.com
console.log(url.hostname); // www.example.com
console.log(url.port); // undefined
console.log(url.pathname); // /path/to/page.html
console.log(url.search); // ?query=string       

// 1.4 set the properties of a URL object
url.port = 8080;
console.log(url.href); // https://www.example.com:8080/path/to/page.html?query=string#hash
url.pathname = '/new/path/to/page.html';
console.log(url.href); // https://www.example.com:8080/new/path/to/page.html?query=string#hash
url.search = '?new=query';



// 2 URLSearchParams object
const searchParams = new URLSearchParams(url.search);
console.log(searchParams.get('query')); // string
searchParams.set('key', 'value');
console.log(searchParams.toString()); // query=string&key=value
searchParams.delete('query');// 删除参数
console.log(searchParams.toString()); // key=value  

searchParams.append('gender', 'male'); // 添加参数
console.log(searchParams.toString()); // 输出: name=John&age=30&gender=male

searchParams.set('age', '31'); // 修改参数
console.log(searchParams.toString()); 


searchParams.set('age', '30'); // 修改参数
console.log(searchParams.toString()); 

// 3 querystring module
querystring.parse(url.search); // 输出: { query: 'string', key: 'value' }

querystring.stringify({ query: 'string', key: 'value' }); // 输出: query=string&key=value
querystring.parse('name=John&age=30&gender=male'); // 输出: { name: 'John', age: '30', gender:'male' }


